<?php

namespace app\api\controller\kuyou\applet;

use think\Db;

class AccountsReceivable extends Common
{
    public function index()
    {
        if ($this->request->isPost()) {
            $where = [];
            $purchase_time = $this->request->post('purchase_time');
            if(!empty($purchase_time)){
                $str = explode('~',$purchase_time);
                if(isset($str[0]) && isset($str[1])){
                    $where['purchase_time'] = ["BETWEEN", [strtotime($str[0]),strtotime($str[1])]];
                }
            }
            $staff_member_id = $this->request->post('staff_member_id');
            if(!empty($staff_member_id)){
                $where['staff_member_id'] = $staff_member_id;
            }

            $map = [];
            $customers_id = $this->request->post('customers_id');
            if(!empty($customers_id)){
                $map['sp.id'] = trim($customers_id);
            }

            $sup = Db::name('kuyou_customers')->alias('sp')
                ->join('kuyou_customertype gt','sp.customertype_id=gt.id')
                ->where($map)
                ->field('sp.id,sp.opening_prepayment,sp.customer_code,sp.customer_name,sp.receive_account,gt.type_name')
                ->order('sp.customer_code asc')
                ->select();

            $field = empty(input('field'))?'createtime':trim(input('field'));
            $order = empty(input('sort'))?'ASC':trim(input('sort'));

            //获取设置
            $set= \think\Db::name('kuyou_sets')->field('type,name,value,is_checked,set')->select();
            $setList = [];
            foreach ($set as $set_k=>$set_v){
                if(!empty($set_v['set'])){
                    $set_v['set'] = unserialize($set_v['set']);
                }
                $setList[$set_v['name']] = $set_v;
            }
            //数量价格格式化
            $price_decimal  = $setList['price_decimal']['value'];
            $stmen = Db::name('kuyou_staff_member')->column('name','id');


            $listarr = [];
            $all_balance = 0;
            $all_receivable = 0;
            $all_advance = 0;
            foreach ($sup as $sup_k=>$sup_v){
                $balance = round($sup_v['receive_account']-$sup_v['opening_prepayment'],$price_decimal);
                $receivable = 0;
                $advance = 0;
                $listarr[] = [
                    'customer_name' => $sup_v['customer_name'],
                    'purchase_time' => '',
                    'purchase_code' => '期初余额',
                    'business_type' => '',
                    's_type'        => 1,
                    'receivable'    => '',
                    'advance'       => '',
                    'balance'       => $balance,
                    'staff_member'  => '',
                    'remark'        => ''
                ];

                $list = Db::name('kuyou_accounts_receivable')
                    ->where($where)
                    ->where(['customers_id'=>$sup_v['id']])
                    ->order([$field=>$order])
                    ->select();
                foreach ($list as $list_k=>$list_v){
                    $balance    += $list_v['receivable'];
                    $balance    -= $list_v['advance'];
                    $receivable += $list_v['receivable'];
                    $advance    += $list_v['advance'];
                    $listarr[] = [
                        'customer_name' => $sup_v['customer_name'],
                        'purchase_time' => !empty($list_v['purchase_time'])?date('Y-m-d',$list_v['purchase_time']):'',
                        'purchase_code' => $list_v['purchase_code'],
                        'business_type' => $list_v['business_type'],
                        's_type'        => 1,
                        'receivable'    => $list_v['receivable'],
                        'advance'       => $list_v['advance'],
                        'balance'       => round($balance,$price_decimal),
                        'staff_member'  => isset($stmen[$list_v['staff_member_id']])?$stmen[$list_v['staff_member_id']]:'',
                        'remark'        => $list_v['remark'],
                    ];
                }
                $listarr[] = [
                    'customer_name' => $sup_v['customer_name'],
                    'purchase_time' => '',
                    'purchase_code' => '小计',
                    'business_type' => '',
                    's_type'        => 2,
                    'receivable'    => round($receivable,$price_decimal),
                    'advance'       => round($advance,$price_decimal),
                    'balance'       => round($balance,$price_decimal),
                    'staff_member'  => '',
                    'remark'        => ''
                ];
                $all_balance    += $balance;
                $all_receivable += $receivable;
                $all_advance    += $advance;
            }

            $counts = [];
            $counts['customer_name'] = '';
            $counts['purchase_time'] = '';
            $counts['purchase_code'] = '合计';
            $counts['business_type'] = '';
            $counts['s_type'] = 3;
            $counts['receivable'] = $all_receivable;
            $counts['advance'] = $all_advance;
            $counts['balance'] = $all_balance;
            $counts['staff_member'] = '';
            $counts['remark'] = '';

            $listarr[] = $counts;

            $result = array("count" => sizeof($listarr), "list" => $listarr);

            if(isset($result['list']) && isset($result['count'])){
                $this->success('查询成功',$result,200);
            }else{
                $this->error('查询失败','',406);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }
}
